/**
 * Copyright © 2019 dream horse Info. Tech Ltd. All rights reserved.
 *
 * @Package: com.gitee.mybatis.fl.convert
 * @author: flying-cattle
 * @date: 2019年4月9日 下午8:15:25
 */
package com.zm.mybatis.gen.generator;

import com.zm.mybatis.gen.entity.PropertyInfo;
import com.zm.mybatis.gen.entity.ResultJson;
import com.zm.mybatis.gen.entity.ServerBasisInfo;
import com.zm.mybatis.gen.util.FreemarkerUtil;
import lombok.Setter;

import java.util.ArrayList;
import java.util.List;

/**
 * Copyright: Copyright (c) 2019 
 *
 * <p>说明：  获取文件路径调用创建文件</P>
 * @version: v3.0.0
 * @author: flying-cattle
 *
 * Modification History:
 * Date         	Author          Version          Description
 *---------------------------------------------------------------*
 * 2019年4月9日      		flying-cattle   v3.0.0           initialize
 */
public class ServerJavaGenerator extends ParentGenerator {
    public String SRC_MAIN_JAVA = "src.main.java.";
    public String SRC_MAIN_TEST = "src.test.java.";
    //路径信息
//    public String ENTITY = "entity";
    public String DAO;
    public String DAO_IMPL = "daoImpl";
    public String SERVICE = "service";
    public String SERVICE_IMPL = "serviceImpl";
    public String CONTROLLER = "controller";
    public String SWAGGER_CONFIG = "swaggerConfig";

    @Setter
    private FreemarkerUtil freemarkerUtil;

    //①创建实体类
    public ResultJson createEntity(String url, ServerBasisInfo bi) {
        String packageUrl = SRC_MAIN_JAVA + bi.getEntityUrl();
        String fileUrl = url + pageToUrl(packageUrl) + bi.getEntityName() + ".java";
        return freemarkerUtil.createFile(bi, "entity.ftl", fileUrl);
    }

    //①创建实体类 response
    public ResultJson createEntityResponse(String url, ServerBasisInfo bi) {
        String packageUrl = SRC_MAIN_JAVA + bi.getEntityResponseUrl();
        String fileUrl = url + pageToUrl(packageUrl) + bi.getEntityResponseName() + ".java";
        return freemarkerUtil.createFile(bi, "entityResponse.ftl", fileUrl);
    }

    //①创建实体类 response
    public ResultJson createEntityRequest(String url, ServerBasisInfo bi) {
        String packageUrl = SRC_MAIN_JAVA + bi.getEntityRequestUrl();
        String fileUrl = url + pageToUrl(packageUrl) + bi.getEntityRequestName() + ".java";
        return freemarkerUtil.createFile(bi, "entityRequest.ftl", fileUrl);
    }

    //②创建DAO
    public ResultJson createDao(String url, ServerBasisInfo bi) {
        DAO = bi.getDaoClassNameSuffix(); // 把Dao 的结尾换成 mapper
        String fileUrl = getGeneratorFileUrl(url, bi.getDaoUrl(), bi.getEntityName(), DAO);
        return freemarkerUtil.createFile(bi, "dao.ftl", fileUrl);
    }

    //③创建mapper配置文件
    public ResultJson createDaoImpl(String url, ServerBasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getMapperUrl(), bi.getEntityName(), DAO_IMPL);
        List<PropertyInfo> list = bi.getCis();
        String agile = "";
        String agileValueFields = "";
        String methodParams = "";
        String saveMethodParamsIn = "";

        List<String> espects = new ArrayList<>();
        espects.add("creatorId");
        espects.add("createId");
        espects.add("updateId");
        espects.add("updatorId");
        espects.add("isDeleted");
        espects.add("updatorId");
        espects.add("createTime");
        espects.add("updateTime");
        espects.add("tenantId");
        for (PropertyInfo propertyInfo : list) {
            if(!"id".equalsIgnoreCase(propertyInfo.getProperty())) {
                agile = agile + propertyInfo.getColumn() + ", ";
                agileValueFields = agileValueFields + "#{item." + propertyInfo.getProperty() + "}" + ", ";
            }

            if(!espects.contains(propertyInfo.getProperty())) {
                methodParams = methodParams + "" + propertyInfo.getJavaType() + " " + propertyInfo.getProperty() + ", ";
                saveMethodParamsIn = saveMethodParamsIn + "request."+propertyInfo.getPropertyGetMethod() + "(), ";
            }
        }
        agile = agile.substring(0, agile.length() - 2);
        agileValueFields = agileValueFields.substring(0, agileValueFields.length() - 2);


        // 给 methodParams 加个 now / operatorId
        methodParams = methodParams + "Long tenantId, Long operatorId";
        saveMethodParamsIn = saveMethodParamsIn + "tenantId, operatorId";
//        methodParams = methodParams.substring(0, methodParams.length() - 2);
//        saveMethodParamsIn = saveMethodParamsIn.substring(0, saveMethodParamsIn.length() - 2);
        bi.setAgile(agile);
        bi.setAgileValueFields(agileValueFields);
        bi.setSaveMethodParamsIn(saveMethodParamsIn);
        bi.setMethodParams(methodParams);
        return freemarkerUtil.createFile(bi, "mapper.ftl", fileUrl);
    }

    //④创建SERVICE
    public ResultJson createService(String url, ServerBasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getServiceUrl(), bi.getEntityName(), SERVICE);
        return freemarkerUtil.createFile(bi, "service.ftl", fileUrl);
    }

    //⑤创建SERVICE_IMPL
    public ResultJson createServiceImpl(String url, ServerBasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getServiceImplUrl(), bi.getEntityName(), SERVICE_IMPL);
        return freemarkerUtil.createFile(bi, "serviceImpl.ftl", fileUrl);
    }

    //⑥创建CONTROLLER
    public ResultJson createController(String url, ServerBasisInfo bi) {
//		createAbstractController( url, bi); //保证父类一直存在
        String fileUrl = getGeneratorFileUrl(url, bi.getControllerUrl(), bi.getEntityName(), CONTROLLER);
        return freemarkerUtil.createFile(bi, "controller.ftl", fileUrl);
    }

    //⑦创建抽象的CONTROLLER
    public ResultJson createAbstractController(String url, ServerBasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getAbstractControllerUrl(), "Abstract", CONTROLLER);
        return freemarkerUtil.createFile(bi, "AbstractController.ftl", fileUrl);
    }

    //⑧创建CONTROLLER
    public ResultJson createSwaggerConfig(String url, ServerBasisInfo bi) {
        String fileUrl = getGeneratorFileUrl(url, bi.getSwaggerConfigUrl(), "Swagger", SWAGGER_CONFIG);
        return freemarkerUtil.createFile(bi, "SwaggerConfig.ftl", fileUrl);
    }

    //生成文件路径和名字
    public String getGeneratorFileUrl(String url, String packageUrl, String entityName, String type) {
        if (type.equals("entity")) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + ".java";
        } else if (type.equals(DAO)) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + DAO + ".java";
        } else if (type.equals("daoImpl")) {
            return url + pageToUrl(packageUrl) + entityName + "Mapper.xml";
        } else if (type.equals("service")) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + "Service.java";
        } else if (type.equals("serviceImpl")) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + "ServiceImpl.java";
        } else if (type.equals("controller")) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + "Controller.java";
        } else if (type.equals("swaggerConfig")) {
            packageUrl = SRC_MAIN_JAVA + packageUrl;
            return url + pageToUrl(packageUrl) + entityName + "Config.java";
        }
        return null;
    }

    public ResultJson createMapperTest(String url, ServerBasisInfo bi) {
        String packageUrl = SRC_MAIN_TEST + bi.getMapperTestUrl();
        String fileUrl = url + pageToUrl(packageUrl) + bi.getEntityName() + "MapperTest.java";
        return freemarkerUtil.createFile(bi, "mapperTest.ftl", fileUrl);
    }

    public ResultJson createServiceTest(String url, ServerBasisInfo bi) {
        String packageUrl = SRC_MAIN_TEST + bi.getServiceTestUrl();
        String fileUrl = url + pageToUrl(packageUrl) + bi.getEntityName() + "ServiceTest.java";
        return freemarkerUtil.createFile(bi, "serviceTest.ftl", fileUrl);
    }
}
